
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
resource.AddFile( "materials/vgui/entities/prop_GasPump.vtf" )
resource.AddFile( "materials/vgui/entities/prop_GasPump.vmt" )
include('shared.lua')

ENT.PutItOnPlace = 0 
ENT.PlaceEnt = NULL

--Don't know why but i think this gas pump is really sweeeeeet.
------------------------------------VARIABLES END
function ENT:SpawnFunction( ply, tr )
   
 	if ( !tr.Hit ) then return end 
 	 
 	local SpawnPos = tr.HitPos + tr.HitNormal * 10 
 	 
 	local ent = ents.Create( "sent_GasPump" )
	ent:SetPos( SpawnPos ) 
 	ent:Spawn()
 	ent:Activate() 
 	 
	return ent 
 	 
end

function ENT:Initialize()

	self.Entity:SetModel("models/props_wasteland/gaspump001a.mdl")
	self.Entity:SetColor(255, 255, 255, 255)
	self.Entity:SetOwner(self.Owner)
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)

	self.Entity:SetSolid(SOLID_VPHYSICS)	
    local phys = self.Entity:GetPhysicsObject()
	if(phys:IsValid()) then phys:Wake() end

--Here spawns the prop that should be at the end of the hose.
 	local ent2 = ents.Create( "prop_physics" )
	ent2:SetModel("models/props_wasteland/prison_pipefaucet001a.mdl")
	local Position = self.Entity:GetPos()+((self.Entity:GetRight()*20)+(self.Entity:GetUp()*45))
	ent2:SetPos(Position)
	local EntAng = self.Entity:GetAngles()
	ent2:SetAngles(Angle(0, 0, 90)+EntAng)
 	ent2:SetColor(0, 0, 0, 255)
 	ent2:Spawn()
 self.PlaceEnt = ent2

--I had to use some custom positions since the position of the prop isn't the position where the hose should start.
--The Vector(0,-18,56) is where the hose start on the gas pump and the Vector(0,-7,0) is the pos where the hose
--should end on ent2.
	constraint.Rope( self.Entity, ent2, 0, 0, Vector(0,-18,56), Vector(0,-7,0), 0, 200, 0, 3, "cable/cable2")
	constraint.Weld( self.Entity, ent2, 0, 0, 1000)

	
end
function ENT:PhysicsCollide( data, phys ) 

--If the pipefaucet collides with the gas pump.
	ent = data.HitEntity
			if string.find(ent:GetModel(), "models/props_wasteland/prison_pipefaucet001a.mdl") then
			self.PlaceEnt = ent
			self.PutItOnPlace = 1
			end
end

function ENT:Think()

--Checking if the pipefaucet has collided with the gas pump.
if self.PutItOnPlace == 1 then 
self.PutItOnPlace = 0
--The pipefaucet will automatically be put on the right place(i think that's cool) =D
			local Position = self.Entity:GetPos()+((self.Entity:GetRight()*20)+(self.Entity:GetUp()*45))
			self.PlaceEnt:SetPos(Position)
			local EntAng = self.Entity:GetAngles()
			self.PlaceEnt:SetAngles(Angle(0, 0, 90)+EntAng)
			local StayThere = ent:GetPhysicsObject()
			constraint.Weld( self.Entity, self.PlaceEnt, 0, 0, 1000)

end
end

--If the gas pump is removed it will also remove the pipefaucet.
function ENT:OnRemove()
if self.PlaceEnt:IsValid() then
self.PlaceEnt:Remove()
end
end
